package cn.com.ddapp.ddm.ui;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.CollectionListAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.bean.CollectionBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.widget.pulltorefresh.PullToRefreshLayout;
import cn.com.ddapp.ddm.widget.pulltorefresh.pullableview.PullableListView;
import okhttp3.Call;

public class CollectionActivity extends BaseActivity implements PullToRefreshLayout.OnRefreshListener {

    private static final int ISONLOAD = 2;
    private static final int ISONFRESH = 1;
    private static final int ISSTART =0;
    @BindView(R.id.share_refresh)
    PullToRefreshLayout share_refresh;
    @BindView(R.id.share_list)
    PullableListView share_list;
    @BindView(R.id.empty)
    EmptyLayout emptyLayout;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    private String userId;
    private int stIndex=0;
    CollectionListAdapter collection;
    @Override
    public void initView() {
        titletext.setText("我的收藏");
        back.setOnClickListener(this);

    }
    @Override
    public void initData() {
        userId = SPUtils.getStringData(this, SPUtils.USERID);
        share_refresh.setOnRefreshListener(this);
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                httpData(ISSTART);
            }
        });
        httpData(ISSTART);
        share_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CollectionBean.ListBean listBean = (CollectionBean.ListBean) adapterView.getAdapter().getItem(i);
                Intent intent = new Intent(CollectionActivity.this, CommidContentActivity.class);
                intent.putExtra("type","id");
                intent.putExtra("id", String.valueOf(listBean.getId()));
                startActivity(intent);
            }
        });
    }
    @Override
    public int getContentViewId() {
        return R.layout.activity_collection;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    private void httpData(final int cate) {
        if(cate==ISSTART){emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);}
        OkUtilCallback callback = new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                Gson gson=new Gson();
                CollectionBean collectionListAdapter = gson.fromJson(response, CollectionBean.class);
                if("OK".equals(collectionListAdapter.getResult())){
                    List<CollectionBean.ListBean> list =  collectionListAdapter.getList();
                    stIndex=list.size();
                    if( collection ==null){
                        collection=new CollectionListAdapter(mContext,list);
                    }
                    if(list.size()==0&&cate==0){
                        emptyLayout.setErrorType(EmptyLayout.NODATA);
                        return;
                    }else{
                        if(cate==ISSTART){
                            share_list.setAdapter(collection);
                        }else if(cate==ISONFRESH){
                           // onRef(list);
                            collection.setList(list);
                            collection.notifyDataSetChanged();
                            share_refresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                        }else if(cate==ISONLOAD){
                            collection.addView(list);
                            collection.notifyDataSetChanged();
                            share_refresh.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                        }
                        emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                    }


                }
            }
            @Override
            public void onError(Call call, Exception e, int id) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                if(cate==1){
                    share_refresh.refreshFinish(PullToRefreshLayout.FAIL);
                }else if(cate==2){
                    share_refresh.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
                super.onError(call, e, id);
            }
        };
        OKUtilAPI.post(OKUtilAPI.getCollection, "userId=" + userId+"&st="+stIndex,callback);
        }

    private void onRef(List<CollectionBean.ListBean> list) {
        collection.clear();
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        stIndex=0;
        httpData(ISONFRESH);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        stIndex=stIndex+stIndex;
        httpData(ISONLOAD);
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.back){
            finish();
        }
        super.onClick(view);
    }
}
